import os
import csv

from ftplib import FTP

from base_class import MyBase

FTP_HOST='localhost'
FTP_USER='zhangle'
FTP_PASS='123456'
FTP_PATH='/FTP'
FTP_SYSTEM_NAME=['ii']
RECEIVE_PATH='/home/zhangle/etl/data/original/'

execute_date='2015_11_13'

class Receiver(MyBase):

    def receive_data(self):
        self.info('receiving data start ...')
        self.info('get abs file list...')
        for abs_file in self.get_abs_files():
           self.info('check abs file exists...')
           if self.check_ftp_file(abs_file):
               self.info('get abs file...')
               self.ftp_get_file(abs_file) 
               self.info('get data file...')
               self.get_data_file(abs_file)
        self.info('receive data end.')
               
    def get_data_file(self, abs_file):
        abs_file = os.path.join(RECEIVE_PATH,abs_file)
        tgz_info = {}
        with open(abs_file, 'rb') as csvfile:
            for row in csv.reader(csvfile, delimiter=','):
                tgz_info = {}
                tgz_info['name'] = row[0]
                tgz_info['size'] = row[1]
                tgz_info['create_time'] = row[2]
                tgz_info['md5'] = row[3]
                break
        if tgz_info:
            self.info('get tgz file' + str(tgz_info))
            self.info('check tgz file size...')
            tgz_file_size = self.ftp_get_file_size(tgz_info['name'])
            self.debug('tgz:%s size:%s' % (tgz_info['name'], tgz_file_size))
            if tgz_file_size == int(tgz_info['size']):
                self.debug('tag file size check pass.') 
                self.debug('get tag file %s start...' % tgz_info['name'])
                self.ftp_get_file(tgz_info['name'])
                self.debug('get tag file %s end.' % tgz_info['name'])

    def ftp_get_file_size(self, file_path):
	self.debug('ftp_get_file_size:[%s]' % file_path)
        self.ftp = FTP(FTP_HOST)
        self.ftp.login(FTP_USER, FTP_PASS)
        self.ftp.cwd(FTP_PATH)

        size = self.ftp.size(file_path)

        self.ftp.quit()

        return size

    def ftp_get_file(self, file):
        self.ftp = FTP(FTP_HOST)
        self.ftp.login(FTP_USER, FTP_PASS)
        self.ftp.cwd(FTP_PATH)

        dst = os.path.join(RECEIVE_PATH,file)
        print dst
        self.ftp.retrbinary('RETR ' + file, open(dst, 'wb').write)

        self.ftp.quit()

    def check_ftp_file(self, file_path):
        self.ftp = FTP(FTP_HOST)
        self.ftp.login(FTP_USER, FTP_PASS)
        self.ftp.cwd(FTP_PATH)

        size = self.ftp.size(file_path)

        self.ftp.quit()

        if size:
            self.info('[%s] check ok' % file_path)
            return True
        return False

    def get_abs_files(self):
        for name in FTP_SYSTEM_NAME:
            yield '%s_%s.abs' % (name, execute_date)
        
        
if __name__ == '__main__':
    r = Receiver()
    r.receive_data()
    #r.check_ftp_file('/FTP/GI_2015_11_15.abs')
